package com.example.saveword.save_word;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.example.saveword.R;
import com.example.saveword.utils.SetUserUtils;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainHomeActivity extends AppCompatActivity implements View.OnClickListener{
    BottomNavigationView nav_menu; // 底部导航栏
    private ViewPager viewPager;
    private List<Fragment> list;
    private FragmentAdapter fragmentAdapter;
    private HomeFragment homeFragment;
    private WordBookFragment wordBookFragment;
    private MyFragment myFragment;
    @SuppressLint("StaticFieldLeak")
    public static MainHomeActivity instance = null;
    private ImageView btn_toCalendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_home);
        initView();
        rePaint();
    }

    // 初始化
    private void initView() {
        nav_menu = findViewById(R.id.bottom_nav);
        btn_toCalendar = findViewById(R.id.btn_toCalendar);
        viewPager = findViewById(R.id.main_content);
        // 设置监听器
        btn_toCalendar.setOnClickListener(this);
    }

    // 切换 fragment 要显示的内容
    public void rePaint() {
        homeFragment = new HomeFragment(this);
        wordBookFragment = new WordBookFragment();
        myFragment = new MyFragment();

        viewPager.setOnPageChangeListener(new MyPagerChangeListener()); // 设置页面切换的监听
        list = new ArrayList<>(); // 添加Fragment
        list.add(homeFragment); // 0页面
        list.add(wordBookFragment); // 1页面
        list.add(myFragment); // 2页面

        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(), list); // 创建适配器
        viewPager.setAdapter(fragmentAdapter);
        viewPager.setCurrentItem(0);
        // 设置底部导航栏监听，切换中间内容
        nav_menu.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.navigation_home:
                        viewPager.setCurrentItem(0);
                        break;
                    case R.id.navigation_review:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.navigation_wordbook:
                        viewPager.setCurrentItem(2);
                        break;
                }
                return true;
            }
        });
    }

    // 监听按钮点击
    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_toCalendar) { // 打开学习日历
            Intent intent = new Intent();
            intent.setClass(this, CalendarActivity.class);
            startActivity(intent);
        }
    }

    class MyPagerChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position){ // 滑动监听
                case 0:
                    nav_menu.setSelectedItemId(R.id.navigation_home);
                    break;
                case 1:
                    nav_menu.setSelectedItemId(R.id.navigation_review);
                    break;
                case 2:
                    nav_menu.setSelectedItemId(R.id.navigation_wordbook);
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}